<?php
/**
 * 版权所有: 合肥讯邦网络科技有限公司 [http://www.ahxb.cc]
 * 公司电话: 0551-63668080
 * 作    者: Ghost.42
 * 修改时间: 2017-07-28 8:49
 * 功能说明: 公共控制器
 */

namespace Home\Controller;
use Think\Controller;
use XBCommon\XBCache;
use Admin\Controller\Message\MessageController;
use XBCommon;
class CommonController extends HomeController
{
    const S_TABLE='sys_sms';
    const C_TABLE='sms_code';

    /**
     * 生成验证码
     */
    public function selfverify(){
        $config =    array(
            'fontSize'    =>    30,    // 验证码字体大小
            'length'      =>    4,     // 验证码位数
            'useNoise'    =>    false, // 关闭验证码杂点
        );
        $Verify =     new \Think\Verify($config);
        $Verify->codeSet = '0123456789';
        ob_end_clean();
        $Verify->entry();
    }

    /**
     * 获取手机验证码
     */
    public function getcode(){
    	
        $mobile = I("post.mobile",'','trim');
        $check=I("post.check",'','trim');
        $yzm = I("post.code",'','trim');//图形验证码

        //尝试在cookie里取手机号码
        if(!$mobile){
            $mobile=cookie('checkArr')['m'];
        }

        if(!$mobile){
            $this->ajaxReturn(0,'手机号不能为空');
        }elseif(!is_mobile($mobile)){
            $this->ajaxReturn(0,'手机号格式不正确');
        }

        if($yzm){
            $verify = new \Think\Verify();
            $res = $verify->check($yzm);
            if(!$res){
                $this->ajaxReturn(0,"图形验证码不正确，请重新输入!");
            }
        }

        if($check==1){  //注册验证身份
            $where['Mobile']=$mobile;
            $where['IsDel']=0;
            $find = M('mem_info')->field('ID')->where($where)->find();
            if($find){
                $this->ajaxReturn(3,"该手机号码已注册过会员，请下载app直接登录！");
            }
        }

        // $message = new \XBCommon\XBMessage($mobile,$msg);
        // $res = $message->send_message();
        // if(!$res){
        //     $this->ajaxReturn(0,'发送短信异常，请稍后重试！');
        // }
        //$result = send_message($mobile,$msg);
        $result = $this->sendMsg($mobile);
//p($result);die;
        //$result=json_decode($res,true);
        if($result['result']=='success'){
            $data=array("ObjectID"=>$mobile,"Type"=>1,"Mode"=>1,"SendMess"=>$result['tmep'],"Status"=>1,"SendTime"=>date("Y-m-d H:i:s"),"Obj"=>1);
            M(self::S_TABLE)->add($data);

            $res=M(self::C_TABLE)->where(array("Name"=>$mobile,"Type"=>0))->find();
            if($res){
                M(self::C_TABLE)->where(array("Name"=>$mobile))->save(array("Code"=>$result['code'],"UpdateTime"=>date("Y-m-d H:i:s")));
            }else{
                $datas=array("Name"=>$mobile,"Type"=>0,"Code"=>$result['code'],"UpdateTime"=>date("Y-m-d H:i:s"));
                $res = M(self::C_TABLE)->add($datas);
				\Think\Log::record('AAAAAAAAAAA');
				\Think\Log::record($res);
            }
            cookie('yzm',array('yzm'=>$yzm),0);
            $this->ajaxReturn(1,'发送成功,请注意查收！');
        }else{
            $this->ajaxReturn(2,'发送失败,请重新尝试！');
        }
    }

    public function getImgCode(){
        $config =    array(
            'fontSize'    =>    30,    // 验证码字体大小
            'length'      =>    4,     // 验证码位数
            'useNoise'    =>    false, // 关闭验证码杂点
        );
        $Verify =     new \Think\ImgCode($config);
        $Verify->codeSet = '0123456789';
        ob_end_clean();
        $Verify->entry();
    }

    /**
     * 发送短信
     * @param integer $mobile 手机号
     * @return array
     */
    public function sendMsg($mobile){

        //获取并处理短信模板并向用户推送审核失败的短信提醒
        $where['EName'] = 'reg_cdoe';
        $where['Status'] = 1;
        $msgTemplate = M('sys_smstemplates')->where($where)->find();
        if (!empty($msgTemplate) && isset($msgTemplate['Content'])) {
            $code = rand(000000,999999);
            $msgTemplate['Content'] = str_replace('#code#',$code,$msgTemplate['Content']);
            $msgObj = new MessageController('yunpiancode');
            $result = $msgObj->send_msg($mobile,$msgTemplate['Content']);
            $result = json_decode($result,true);
            if(isset($result['code']) && $result['code'] === 0){
                return ['result' => 'success','msg' => '发送成功','tmep' => $msgTemplate['Content'],'code' => $code];
            }else{
                return ['result' => 'error','msg' => $result['msg']];
            }
        }else{
            return ['result' => 'error','msg' => '获取短信模板失败'];
        }
    }

}